﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports GenDotNet.Task.Common
Imports GenDotNet.ObjectModel
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">

Namespace GenInputMetadata
   Public Class ChildData
      Inherits BaseData
      Implements IChildData

      Private mChildKeys As List(Of IKeyData)
      Private mProperties As List(Of IPropertyData)

      Public Shared Function Load( _
                  ByVal objectData As ObjectData, _
                  ByVal element As XElement) _
                  As ChildData
         Dim ret = New ChildData(objectData.RootData, element)
         Dim xDoc = element.Document
         ret.mChildKeys = KeyData.Load(objectData.RootData, From x In element.<orm:ChildKeyField>)
         Return ret
      End Function

      Public Shared Function Load( _
                  ByVal objectData As ObjectData, _
                  ByVal elements As IEnumerable(Of XElement)) _
                  As List(Of IChildData)
         Dim ret = New List(Of IChildData)
         For Each XElement In elements
            ret.Add(Load(objectData, XElement))
         Next
         Return ret
      End Function

      Private Sub New(ByVal rootData As RootData, _
                      ByVal element As XElement)
         MyBase.New(rootData, element)
      End Sub

#Region "IChildData implementation"
      Public ReadOnly Property Caption() _
               As String _
               Implements IChildData.Caption
         Get
            Return Element.@Caption
         End Get
      End Property

      Public ReadOnly Property IsHierarchy() _
               As Boolean _
               Implements IChildData.IsHierarchy
         Get
            Return Element.@IsHierarchy.IsTrue
         End Get
      End Property

      Public ReadOnly Property Name() _
               As String _
               Implements IChildData.Name
         Get
            Return Element.@Name
         End Get
      End Property

      Public ReadOnly Property UniqueName() _
               As String _
               Implements IChildData.UniqueName
         Get
            Return Element.@UniqueName
         End Get
      End Property

      Public ReadOnly Property PluralUniqueName() _
               As String _
               Implements IChildData.PluralUniqueName
         Get
            Return Element.@PluralUniqueName
         End Get
      End Property

      Public ReadOnly Property ChildFlag() _
               As UInteger _
               Implements IChildData.ChildFlag
         Get
            Return CUInt(Element.@ChildFlag)
         End Get
      End Property

      Public ReadOnly Property ObjectData() _
               As IObjectData _
               Implements IChildData.ObjectData
         Get
            Return RootData.GetObject(Element.@ObjectName)
         End Get
      End Property

      Public ReadOnly Property ChildKeys() _
               As System.Collections.Generic.List(Of IKeyData) _
               Implements IChildData.ChildKeys
         Get
            Return mChildKeys
         End Get
      End Property
#End Region

   End Class
End Namespace